unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, OleCtrls, ZKFPEngXControl_TLB, ComCtrls, variants,
  ExtDlgs;

type
  TForm1 = class(TForm)
    pnlCommand: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    btnInit: TButton;
    EDSensorNum: TEdit;
    EDSensorIndex: TEdit;
    EDSensorSN: TEdit;
    RGFormat: TRadioGroup;
    btnSaveImage: TButton;
    btnReg: TButton;
    EDFingerName: TEdit;
    btnVerify: TButton;
    Button1: TButton;
    StatusBar: TStatusBar;
    btnDogRead: TButton;
    EDInfo1: TEdit;
    btnDogWrite: TButton;
    EDInfo: TEdit;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button6: TButton;
    Button5: TButton;
    ZKFPEngX1: TZKFPEngX;
    Edit1: TEdit;
    Label5: TLabel;
    Label6: TLabel;
    Edit2: TEdit;
    OpenPictureDialog1: TOpenPictureDialog;
    SavePictureDialog1: TSavePictureDialog;
    Button7: TButton;
    Button8: TButton;
    procedure btnInitClick(Sender: TObject);
    procedure ZKFPEngX1Capture(Sender: TObject; ActionResult: WordBool;
      ATemplate: OleVariant);
    procedure btnSaveImageClick(Sender: TObject);
    procedure btnRegClick(Sender: TObject);
    procedure ZKFPEngX1FeatureInfo(Sender: TObject; AQuality: Integer);
    procedure btnVerifyClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ZKFPEngX1Enroll(Sender: TObject; ActionResult: WordBool;
      ATemplate: OleVariant);
    procedure ZKFPEngX1ImageReceived(Sender: TObject;
      var AImageValid: WordBool);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure btnDogReadClick(Sender: TObject);
    procedure btnDogWriteClick(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure ZKFPEngX1FingerLeaving(Sender: TObject);
    procedure ZKFPEngX1FingerTouching(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
  private
    { Private declarations }
    FRegTemplate: oleVariant;
    FMatchType: Integer;
    sRegTemplate: WideString;
    FID: Integer;
    FFingerName: TStringList;
    fpcHandle: Integer;
    iOld: DWORD;

    FAutoIdentify: Boolean;
    DestTemplate:array[0..2047] of Byte;
    SourceTemplate:PByte;

    FTemplateStr: string;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.btnInitClick(Sender: TObject);
begin
  ZKFPENGx1.SetImageDirection(false);
  if ZKFPEngX1.InitEngine = 0 then
    begin
     MessageDlg('Initial success!', mtInformation, [mbOK], 0);
 //    ClientWidth := ZKFPEngX1.ImageWidth + pnlCommand.Width;
//     ClientHeight := ZKFPEngX1.ImageHeight;
     btnInit.Enabled := False;
     FMatchType := 0;
     StatusBar.Panels.Items[0].Text := 'Sensor connected';
     EDSensorNum.Text := IntToStr(ZKFPEngX1.SensorCount);
     EDSensorIndex.Text := IntToStr(ZKFPEngX1.SensorIndex);
     EDSensorSN.Text := ZKFPEngX1.SensorSN;
    end;
end;

procedure TForm1.ZKFPEngX1Capture(Sender: TObject; ActionResult: WordBool;
  ATemplate: OleVariant);
var
  ID, I: Integer;
  Score, ProcessNum: Integer;
  RegChange: wordBool;
  iNew: Integer;
  sTemp, sName: string;
  Num1, Num2: Integer;
  tmp: OleVariant;
  p, p1: Pointer;
  sVerTemplate:WideString;
  begin
//  ZKFPEngX1.SaveTemplate('C:\Ver.tpl', ATemplate);
//  if ZKFPEngX1.VerRegFingerFile('c:\reg.tpl',ATemplate,false,RegChange) then
  StatusBar.Panels.Items[0].Text := 'Acquired fingerprint template';
  if FMatchType = 1 then begin //1:1
     //Test Encode and Decode
     sTemp := ZKFPEngX1.EncodeTemplate1(ATemplate);
     ATemplate := ZKFPEngX1.DecodeTemplate1(sTemp);

     ZKFPEngX1.DecodeTemplate(sRegTemplate, FRegTemplate);
{
     tmp := VarArrayCreate([0, 2287], varByte);
     p  := VarArrayLock(tmp);
     ZeroMemory(p, 2288);
      p1 := VarArrayLock(FRegTemplate);
     CopyMemory(p, p1, TVarData(FRegTemplate).VArray^.Bounds[0].ElementCount);
     VarArrayUnlock(FRegTemplate);
     VarArrayUnlock(tmp);
}
     if ZKFPEngX1.VerFingerFromStr(sRegTemplate,sTemp, false, regChange) then

     //if ZKFPEngX1.VerFinger(Tmp, ATemplate, True, regChange) then
        MessageDlg('Fingerprint verification success!', mtInformation, [mbOK], 0)
     else
        MessageDlg('Fingerprint Verification Failed!', mtInformation, [mbOK], 0);
     //if ZKFPEngX1.VerRegFingerFile('c:\reg.tpl', ATemplate, False, regChange) then
     //   MessageDlg('Fingerprint verification success!', mtInformation, [mbOK], 0)
     //else
     //   MessageDlg('Fingerprint Verification Failed!', mtInformation, [mbOK], 0);
  end else if FMatchType = 2 then begin //1:N
     iNew := GetTickCount - iOld;
     if not FAutoIdentify then begin
        Score := 8;
        ID := ZKFPEngX1.IdentificationFromStrInFPCacheDB(fpcHandle, ZKFPEngX1.GetTemplateAsString, Score, ProcessNum);
        if ID = -1 then
           MessageDlg('Fingerprint verification Failed! Score = ' + IntToStr(Score) + ' Processed Number = ' + IntToStr(ProcessNum), mtInformation, [mbOK], 0)
        else begin
           for I := 0 to FFingerName.Count - 1 do
             if ID = Integer(FFingerName.Objects[I]) then
                MessageDlg(Format('Fingerprint identification success! Name= %s Score = %d Processed Number = %d time = %d',
                   [FFingerName.Strings[I], Score, ProcessNum, iNew]), mtInformation, [mbOK], 0)
        end;
     end else begin
        ID := ATemplate[0];
        Score := ATemplate[1];
        if ID = -1 then
           sTemp := 'Fingerprint verification Failed! '
        else
           sTemp := 'Fingerprint identification success!';
        Num1 := ATemplate[2];
        Num2 := ATemplate[3];
        for I := 0 to FFingerName.Count - 1 do
          if ID = Integer(FFingerName.Objects[I]) then
              sName := FFingerName.Strings[I];
        MessageDlg(sTemp + Format(' Name= %s Score = %d Processed Number = %d | %d  time = %d',
              [sName, Score, num1, num2, iNew]), mtInformation, [mbOK], 0);
     end;
  end;
end;

procedure TForm1.btnSaveImageClick(Sender: TObject);
var
  sFileName :string;
begin
  sFileName:= 'C:\Fingerprint';

  if RGFormat.ItemIndex = 0 then
        sFileName:=sFileName+'.bmp'
  else if RGFormat.ItemIndex = 1 then
     sFileName:=sFileName + '.jpg';
   SavePictureDialog1.FileName:=sFileName;
     //  MessageDlg('File saved as (' + sFileName + ')!', mtConfirmation, [mbOK], 0);
  if SavePictureDialog1.Execute then
  begin
    sFileName:=SavePictureDialog1.FileName;
       if RGFormat.ItemIndex = 0 then
     ZKFPEngX1.SaveBitmap(sFileName )
  else if RGFormat.ItemIndex = 1 then
     ZKFPEngX1.SaveJPG(sFileName );
   end;

     end;

procedure TForm1.btnRegClick(Sender: TObject);
begin
//ZKFPEngX1.EnrollCount:=StrToINt(Edit1.Text);
  if EDFingerName.Text = '' then begin
     MessageDlg('Please enter ID!', mtInformation, [mbOK], 0);
     Exit;
  end;
    ZKFPEngX1.CancelEnroll;
  ZKFPENgX1.EnrollCount:=StrToINt(Edit1.Text);
  ZKFPEngX1.BeginEnroll;
  StatusBar.Panels.Items[0].Text := 'Begin Register';
end;

procedure TForm1.ZKFPEngX1FeatureInfo(Sender: TObject; AQuality: Integer);
var
  sTemp: string;
begin
  sTemp := '';
  if ZKFPEngX1.EnrollIndex<>1 then
    if ZKFPEngX1.IsRegister then
     sTemp := 'Register status: still press finger ' + IntToStr(ZKFPEngX1.EnrollIndex-1) + ' times!';
  sTemp := sTemp + ' Fingerprint quality';

  if AQuality <> 0 then
     sTemp := sTemp + ' not good'
  else
     sTemp := sTemp + ' good';
  StatusBar.Panels.Items[0].Text := sTemp;
end;

procedure TForm1.btnVerifyClick(Sender: TObject);
begin
  if ZKFPEngX1.IsRegister then ZKFPEngX1.CancelEnroll;
  StatusBar.Panels.Items[0].Text := 'begin verification(1:1)';
  FMatchType := 1;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if ZKFPEngX1.IsRegister then ZKFPEngX1.CancelEnroll;
  StatusBar.Panels.Items[0].Text := 'begin identification(1:N)';
  FMatchType := 2;
end;

procedure TForm1.ZKFPEngX1Enroll(Sender: TObject; ActionResult: WordBool;
  ATemplate: OleVariant);
var
  i: Integer;
begin
  if not ActionResult then
     MessageDlg('Fingerprint register failed', mtWarning, [mbOK], 0)
  else begin
     MessageDlg('Fingerprint register success£¡', mtWarning, [mbOK], 0);
     ZKFPEngX1.EncodeTemplate(ATemplate, sRegTemplate);
//     i := Length(sRegTemplate);
  //   ZKFPEngX1.SaveTemplate('c:\reg.tpl', ATemplate);
     //if FID mod 2 = 1 then
     //   ZKFPEngX1.AddRegTemplateToFPCacheDB(fpcHandle, FID, ATemplate)
     //else
     //   ZKFPEngX1.AddRegTemplateFileToFPCacheDB(fpcHandle, FID, 'c:\fingerprint.tpl');

     ZKFPEngX1.ModifyTemplateStr(sRegTemplate, True);

     ZKFPEngX1.AddRegTemplateStrToFPCacheDB(fpcHandle, FID, sRegTemplate);

     //ZKFPEngX1.RemoveRegTemplateFromFPCacheDB(fpcHandle, FID);
     //ZKFPEngX1.AddRegTemplateToFPCacheDB(fpcHandle, FID, ATemplate);

     FFingerName.AddObject(EDFingerName.Text, TObject(FID));
     Inc(FID);
  end;
end;

procedure TForm1.ZKFPEngX1ImageReceived(Sender: TObject;
  var AImageValid: WordBool);
begin
  with ZKFPEngX1 do
    PrintImageAt(Self.Canvas.Handle, 0, 0, ImageWidth, ImageHeight);
  //AImageValid := False;
  iOld := GetTickCount;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FID := 1;
  FFingerName := TStringList.Create;
  fpcHandle := ZKFPEngX1.CreateFPCacheDB;
  FRegTemplate := null;
  FAutoIdentify := False;
//  FFingerName.AddObject(EDFingerName.Text, TObject(FID));
//  ZKFPEngX1.AddRegTemplateFileToFPCacheDB(fpcHandle, 1, 'c:\reg.tpl');
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FFingerName.Free;
  ZKFPEngX1.FreeFPCacheDB(fpcHandle);
end;

procedure TForm1.btnDogReadClick(Sender: TObject);
var
  p1, p2: Integer;
  v: OleVariant;
  p: array [0..23] of Char;
  ap: Pointer;
begin
  if ZKFPEngX1.DongleIsExist then
  begin
  p1 := 0;
  p2 := 23;
  ZKFPEngX1.DongleMemRead(p1, p2, v);
  ap := VarArrayLock(v);
  CopyMemory(@p[0], ap, p2);
  p[23] := #0;
  EDInfo1.Text := p;
  end
  else
  ShowMessage('¼ÓÃÜ¹·²»´æÔÚ');


  end;

procedure TForm1.btnDogWriteClick(Sender: TObject);
var
  p1, p2: Integer;
  v: OleVariant;
  i: Integer;
  s: string;
begin
  p1 := 0;
  p2 := Length(EDInfo.Text);
  s := EDInfo.Text;
  v := VarArrayCreate([0, p2 - 1], varByte);
  for i := 0 to Length(EDInfo.Text) - 1 do
    v[i] := Ord(s[i + 1]);
  ZKFPEngX1.DongleMemWrite(p1, p2, v);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  ZKFPEngX1.CancelCapture;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  ZKFPEngX1.BeginCapture;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  ZKFPEngX1.EndEngine;
  btnInit.Enabled := True;
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
  FAutoIdentify := not FAutoIdentify;
  ZKFPEngX1.SetAutoIdentifyPara(FAutoIdentify, fpcHandle, 8);
end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  ZKFPEngX1.CancelCapture;
end;

procedure TForm1.ZKFPEngX1FingerLeaving(Sender: TObject);
begin
  StatusBar.Panels.Items[1].Text := 'Leaving';
end;

procedure TForm1.ZKFPEngX1FingerTouching(Sender: TObject);
begin
  StatusBar.Panels.Items[1].Text := 'Touching';
end;

procedure TForm1.Button5Click(Sender: TObject);
var
  bReg: WordBool;
  sFileName:string;
  begin
if OpenPictureDialog1.Execute then
 begin
sFileName:=OpenPictureDialog1.FileName;
 ZKFPEngX1.AddImageFile(sFileName,StrToINt(Edit2.Text));
end;
end;

procedure TForm1.Button7Click(Sender: TObject);
var
  sFileName:string;
  pic:TPicture;
  bmp:TBitmap;
  begin
  pic:=TPicture.Create;
  bmp:=Tbitmap.Create;
if OpenPictureDialog1.Execute then
 begin
sFileName:=OpenPictureDialog1.FileName;
pic.LoadFromFile(sFileName);
bmp.Assign(pic.Graphic);
 ZKFPEngX1.AddBitmap(bmp.Handle,0,0,bmp.Width,bmp.Height,StrToInt(Edit2.Text));
 pic.Free;
 bmp.Free;
end;


end;

procedure TForm1.Button8Click(Sender: TObject);
begin
ZKFPENgX1.SaveBitmap('c:\fingerprint.bmp');
end;

end.
